package com.xjscrm.server.service.common.impl;

import com.uzai.trace.TraceGenerate;
import com.xjscrm.common.entity.Merchant;
import com.xjscrm.common.utils.DateUtil;
import com.xjscrm.server.mapper.MerchantMapper;
import com.xjscrm.server.service.common.XxlJobSyncService;
import com.xjscrm.server.service.customer.WwCustomerSyncService;
import com.xjscrm.server.service.friendreq.OperatePlanDeviceSyncService;
import com.xjscrm.server.service.grouppull.GroupPullMarketingService;
import com.xjscrm.server.service.grouppulljob.GroupPullJobSyncService;
import com.xjscrm.server.service.grouppulljob.GroupPullTaskSyncService;
import com.xjscrm.server.service.groupsendjob.GroupSendJobSyncService;
import com.xjscrm.server.service.groupsendjob.GroupSendTaskSyncService;
import com.xjscrm.server.service.qywxcallback.QywxCallbackService;
import com.xjscrm.server.service.soptask.SopJobMsgService;
import com.xjscrm.server.service.soptask.SopJobSyncService;
import com.xjscrm.server.service.soptask.SopTaskSyncService;
import com.xjscrm.server.service.wwgrouptags.WwGroupTagsSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author liuqi
 * @date 2021年08月25日 20:17
 */
@Service
@Slf4j
public class XxlJobSyncServiceImpl implements XxlJobSyncService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private GroupSendTaskSyncService groupSendTaskSyncService;
    @Autowired
    private GroupSendJobSyncService groupSendJobSyncService;
    @Autowired
    private GroupPullMarketingService groupPullMarketingService;
    @Autowired
    private GroupPullTaskSyncService groupPullTaskSyncService;
    @Autowired
    private GroupPullJobSyncService groupPullJobSyncService;
    @Autowired
    private WwGroupTagsSyncService wwGroupTagsSyncService;
    @Autowired
    private SopTaskSyncService sopTaskSyncService;
    @Autowired
    private SopJobSyncService sopJobSyncService;
    @Autowired
    private SopJobMsgService sopJobMsgService;
    @Autowired
    private OperatePlanDeviceSyncService operatePlanDeviceSyncService;
    @Autowired
    private WwCustomerSyncService wwCustomerSyncService;
    @Autowired
    private QywxCallbackService qywxCallbackService;

    /**
     * 执行5秒一次的循环任务
     *
     * @param
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Async("taskExecutorForXxJob")
    public void executeBy5s() {

        //限流key
        String rateLimitKey = "-executeBy5s-taskLimitKey-";
        Object object = redisTemplate.opsForValue().get(rateLimitKey);
        if (object == null) {
            redisTemplate.opsForValue().set(rateLimitKey, rateLimitKey, 300, TimeUnit.SECONDS);
        } else {
            return;
        }

        log.info("-----调用每隔5秒执行的线程----");

        try {
            //查询当前所有开启的自动发圈的配置方案
            int now = DateUtil.getTime();

            //先查询运营商配置的数据库连接
            List<Merchant> merchantList = merchantMapper.selectByList();
            //循环遍历该运营商列表，
            if (merchantList != null && merchantList.size() > 0) {
                for (Merchant merchant : merchantList) {

                }
            }
        } catch (Exception e) {
            log.info(e.getMessage(), e);
        } finally {
            //删除限流key
            redisTemplate.delete(rateLimitKey);
        }
    }

    /**
     * 执行10秒一次的循环任务
     *
     * @param
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Async("taskExecutorForXxJob")
    public void executeBy10s() {

        //限流key
        String rateLimitKey = "-executeBy10s-taskLimitKey-";
        Object object = redisTemplate.opsForValue().get(rateLimitKey);
        if (object == null) {
            redisTemplate.opsForValue().set(rateLimitKey, rateLimitKey, 300, TimeUnit.SECONDS);
        } else {
            return;
        }

        log.info("-----调用每隔10秒执行的线程----");

        try {
            //查询当前所有开启的自动发圈的配置方案
            int now = DateUtil.getTime();

            //先查询运营商配置的数据库连接
            List<Merchant> merchantList = merchantMapper.selectByList();
            //循环遍历该运营商列表，
            if (merchantList != null && merchantList.size() > 0) {
                for (Merchant merchant : merchantList) {
                    //遍历会员表-补充会员userId信息-创建标签
                    wwCustomerSyncService.autoRefreashExternalUserid(merchant.getId());
                    //遍历会员表-补充会员userId信息-发送标签
                    wwCustomerSyncService.autoSendExternalUserid(merchant.getId());
                    //遍历会员表-补充信息-删除过期标签
                    wwCustomerSyncService.autoDeleteExternalUserid(merchant.getId());
                    //处理好友请求发送
                    operatePlanDeviceSyncService.autoHanderWwFriendReq(merchant.getId());
                    //处理第三方企业协议
                    //qywxCallbackService.handerQywxCallback(merchant.getId());
                }
            }
        } catch (Exception e) {
            log.info(e.getMessage(), e);
        } finally {
            //删除限流key
            redisTemplate.delete(rateLimitKey);
        }
    }

    /**
     * 执行30秒一次的循环任务
     *
     * @param
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    @Async("taskExecutorForXxJob")
    public void executeBy30s() {

        //限流key
        String rateLimitKey = "-executeBy30m-taskLimitKey-";
        Object object = redisTemplate.opsForValue().get(rateLimitKey);
        if (object == null) {
            redisTemplate.opsForValue().set(rateLimitKey, rateLimitKey, 300, TimeUnit.SECONDS);
        } else {
            return;
        }

        log.info("-----调用每隔30分钟执行的线程----");

        try {
            //查询当前所有开启的自动发圈的配置方案
            int now = DateUtil.getTime();

            //先查询运营商配置的数据库连接
            List<Merchant> merchantList = merchantMapper.selectByList();
            //循环遍历该运营商列表，
            if (merchantList != null && merchantList.size() > 0) {
                for (Merchant merchant : merchantList) {
                    //处理消息群发立即和定时任务
                    groupSendTaskSyncService.autoGroupSendTaskSyncByImmediately(merchant.getId());
                    //处理消息群发循环任务
                    groupSendTaskSyncService.autoGroupSendTaskSyncByCycle(merchant.getId());
                    //发送群发消息到MQ
                    groupSendJobSyncService.groupSendJobSendMsg(merchant.getId());

                    //处理消息批量拉群立即和定时任务
                    groupPullTaskSyncService.autoGroupPullTaskSyncByImmediately(merchant.getId());
                    //发送批量拉群数据到MQ
                    groupPullJobSyncService.groupPullJobSendData(merchant.getId());

                    //处理SOP手动任务
                    sopTaskSyncService.autoSopTaskSyncByHander(merchant.getId());
                    //处理SOP任务的阶段任务
                    sopJobSyncService.autoSopJobSyncByStep(merchant.getId());
                    //发送SOP任务的消息
                    sopJobMsgService.sendSopJobMsgToMqSync(merchant.getId());

                    //处理拉群营销数据
                    groupPullMarketingService.autoHanderMarketingData(merchant.getId());

                    //自动群标签
                    wwGroupTagsSyncService.autoWwGroupTagsSync(merchant.getId());



                }
            }
        } catch (Exception e) {
            log.info(e.getMessage(), e);
        } finally {
            //删除限流key
            redisTemplate.delete(rateLimitKey);
        }
    }

}
